package frontend;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logs.LogFactory;

import org.apache.log4j.Logger;

import backend.InputValidation;
import backend.db.ORM;

/**
 * Servlet implementation class CheckRegistration
 */
public class CheckRegistration extends HttpServlet {
  private static final Logger logger = LogFactory.getLog(CheckRegistration.class);
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  @Override protected void doPost(final HttpServletRequest request, final HttpServletResponse response) {
    logger.debug("CheckRegistration");
    // verify field validity
    final String username = request.getParameter("username");
    final String password = request.getParameter("password");
    final String confirm = request.getParameter("confirm");
    final String name = request.getParameter("name");
    final String role = request.getParameter("role");
    final String adminVeri = request.getParameter("adminVeri");
    final String address = request.getParameter("address");
    final String phone = request.getParameter("phone");
    if (InputValidation.validateRegistration(username, password, confirm, name, role, adminVeri, address,
        phone)) {
      ORM.addUser(username, password, role, name, phone, address);
      logger.debug(username + " registered successfully");
    } else {
      logger.debug("registration failed for user " + username);
      response.setStatus(HttpStatus.SC_OK + 1);
    }
  }
}
